package com.appkubes.mall.admin.application.message.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.message.dao.MessageDao;
import com.appkubes.mall.core.po.MallMessage;
import com.appkubes.mall.core.po.MallMessageExample;
import com.appkubes.mall.core.po.MallMessageWithBLOBs;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class MessageService {

    @Autowired
    MessageDao messageDao;

    public PageInfo querySelective(Integer page, Integer size, String sort, String order) {
        MallMessageExample example = new MallMessageExample();
        MallMessageExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        List<MallMessage> list = messageDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public MallMessage findById(String messageKey) {
        return messageDao.selectByPrimaryKey(messageKey);
    }

    public int add(MallMessageWithBLOBs model) {
        model.setMessageKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);

        return messageDao.insertSelective(model);
    }

    public int update(MallMessageWithBLOBs model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return messageDao.updateByPrimaryKeySelective(model);
    }

    public int deleteById(String messageKey) {
        return messageDao.deleteByPrimaryKey(messageKey);
    }

    public int deleteByIds(String[] ids) {
        return messageDao.delete(ids);
    }

    public int batchenable(String[] ids){
        return messageDao.batchenable(ids);
    }

    public int batchunenable(String[] ids){
        return messageDao.batchunenable(ids);
    }


}
